package com.ssyt.hiringtwo.servies.company;

import java.util.ArrayList;
import java.util.List;

import com.ssyt.hiringtwo.common.DBUtil;
import com.ssyt.hiringtwo.entity.PositionEntity;

public class PositionService {
	/**
	 * 查询管理界面的职位管理信息
	 * 
	 * @param talentId
	 * @return
	 */
	public List<PositionEntity> listPositionService(int talentId) {
		// 职位信息查询
        String listpositionSql = "SELECT id, name, publish_time, valid_days FROM position WHERE user_id=? AND NOW() < DATE_ADD(publish_time, INTERVAL valid_days DAY)";
		//String listpositionSql = "SELECT id, name, publish_time, valid_days FROM position WHERE user_id=? AND NOW() < DATE_ADD(p.publish_time, INTERVAL p.valid_days DAY) ";
		List<PositionEntity> positionlist = DBUtil.executeQuery(
				PositionEntity.class, listpositionSql, talentId);
		for (PositionEntity positionEntity : positionlist) {
			int positionId = positionEntity.getId();
			// 对应职位的收到的简历数
			String allResumeDeliveryCountSql = "SELECT COUNT(*) FROM application WHERE position_id=?";
			int allResumeDeliveryCount = DBUtil.getCount(
					allResumeDeliveryCountSql, positionId);
			positionEntity.setAllResumeDeliveryCount(allResumeDeliveryCount);

			// 未读简历数
			String unreadResumeCountSql = "SELECT COUNT(*) FROM application WHERE position_id=? AND status=1";
			int unreadResumeCount = DBUtil.getCount(unreadResumeCountSql,
					positionId);
			positionEntity.setUnreadResumeCount(unreadResumeCount);

			// 未处理的简历数
			String unhandleResumeCountSql = "SELECT COUNT(*) FROM application WHERE position_id=? AND status=2";
			int unhandleResumeCount = DBUtil.getCount(unhandleResumeCountSql,
					positionId);
			positionEntity.setUnhandleResumeCount(unhandleResumeCount);
		}
		return positionlist;
	}

	/**
	 * 添加职位Service
	 * 
	 * @param position
	 */
	public void addPosition(PositionEntity position) {
		String sql = "INSERT INTO position VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ?, ?,NOW())";
		List<Object> paramList = new ArrayList<>();
		paramList.add(position.getUserId());
		paramList.add(position.getName());
		paramList.add(position.getSalaryBegin());
		paramList.add(position.getSalaryEnd());
		paramList.add(position.getProvinceCode());
		paramList.add(position.getCityCode());
		paramList.add(position.getWorkExperience());
		paramList.add(position.getEducation());
		paramList.add(position.getJob());
		paramList.add(position.getDuty());
		paramList.add(position.getTreatment());
		paramList.add(position.getValidDays());
		DBUtil.executeUpdate(sql, paramList);
	}
	   /**
     * 删除职位
     * 
     * @param id
     */
    public int deletePosition(int id) {
        String sql = "DELETE FROM position WHERE id=?";
        return DBUtil.executeUpdate(sql, id);
    }
    /**
     * 更新职位信息
     * @param position
     */
	public void editPosition(PositionEntity position) {
		String sql = "UPDATE position SET name=?, salary_begin=?, salary_end=?, province_code=?, city_code=?, work_experience=?, education=?, job=?, duty=?, treatment=?, valid_days=?, edit_time=NOW() WHERE id=?";
        List<Object> paramList = new ArrayList<>();
        paramList.add(position.getName());
        paramList.add(position.getSalaryBegin());
        paramList.add(position.getSalaryEnd());
        paramList.add(position.getProvinceCode());
        paramList.add(position.getCityCode());
        paramList.add(position.getWorkExperience());
        paramList.add(position.getEducation());
        paramList.add(position.getJob());
        paramList.add(position.getDuty());
        paramList.add(position.getTreatment());
        paramList.add(position.getValidDays());
        paramList.add(position.getId());
  
        
       DBUtil.executeUpdate(sql, paramList);
        
	}

    /**
     * 根据ID查询职位的详情
     * 
     * @param id
     * @return
     */
    public PositionEntity getPosition(int id) {
        String sql = "SELECT * FROM position WHERE id=?";
        return DBUtil.getUniqueResult(PositionEntity.class, sql, id);
    }
    /**
     * 更新职位有效期
     * 
     * @param id
     * @param delayDay
     * @return
     */
    public int updateValidDays(int id, int delayDay) {
        String sql = "UPDATE position SET valid_days=valid_days+?, edit_time=NOW() WHERE id=?";
        return DBUtil.executeUpdate(sql, delayDay, id);
    }
}
